package com.geeaks;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class API {
	
	public static String playListName = "geeaks";
	
	public static String playlist_detail(String id, List<Map<String, Object>> fileList) {
		String url = Util.HOSTURL + "weapi/v3/playlist/detail?csrf_token=";
		String param = "{id:'" + id + "', csrf_token:''}";
		JSONObject result = JSON.parseObject(Util.getJson(param, url));
		if (result.getString("code").contains("404")) {
			System.out.println("解析歌单失败，id:" + id);
			return "";
		}
		JSONObject playlistJson = result.getJSONObject("playlist");
		playListName = playlistJson.getString("name");
		List<Object> playlist = playlistJson.getJSONArray("trackIds");
		StringBuilder ids = new StringBuilder();
		for (Object play : playlist) {
			JSONObject temp = JSON.parseObject(play.toString());
			ids.append(Util.StringToInt(temp.getString("id"))).append(",");
		}
		return song_detail(ids.toString(), fileList);
	}
	
	static String song_detail(String id, List<Map<String, Object>> fileList) {
		String url = Util.HOSTURL + "weapi/v3/song/detail?csrf_token=";
		String[] ids = id.split(",");
		StringBuilder c = new StringBuilder();
		for (String i : ids) {
			c.append("{\"id\":").append(i).append("},");
		}
		String param = "{ids:'[" + id + "]',c:'[" + c.substring(0, c.length() - 1) + "]', csrf_token:''}";
		String json = Util.getJson(param, url);
		JSONObject result = JSON.parseObject(json);
		if (result.getString("code").contains("404")) {
			System.out.println("解析歌曲详情失败，id:" + id);
			return "";
		}
		JSONArray songList = result.getJSONArray("songs");
		return listToIds(songList, fileList);
	}
	
	public static List<Map<String, Object>> song_url(String ids, List<Map<String, Object>> fileList) {
		String url = Util.HOSTURL + "weapi/song/enhance/player/url?csrf_token=";
		String param = "{ids:'[" + ids + "]',br:999000, csrf_token:''}";
		String json = Util.getJson(param, url);
		JSONObject map = JSON.parseObject(json);
		List<Object> songs = map.getJSONArray("data");
		for (Object temp : songs) {
			JSONObject song = JSON.parseObject(temp.toString());
			for (Map<String, Object> aSongList : fileList) {
				if (Util.StringToInt(song.getString("id")) == Util.StringToInt(aSongList.get("id").toString())) {
					aSongList.put("url", song.get("url"));
				}
			}
		}
		return fileList;
	}
	
	private static String listToIds(List<Object> inList, List<Map<String, Object>> outList) {
		StringBuilder ids = new StringBuilder();
		Map<String, Object> songs;
		for (Object temp : inList) {
			JSONObject map = JSON.parseObject(temp.toString());
			songs = new HashMap<String, Object>();
			ids.append(Util.StringToInt(map.getString("id"))).append(",");
			JSONArray ar = map.getJSONArray("ar");
			StringBuilder name = new StringBuilder();
			for (Object temp2 : ar) {
				JSONObject map1 = JSON.parseObject(temp2.toString());
				name.append(map1.getString("name")).append("&");
			}
			songs.put("id", Util.StringToInt(map.get("id").toString()));
			songs.put("name", name.substring(0, name.length() - 1) + "-" + map.get("name"));
			outList.add(songs);
		}
		if (inList.isEmpty()) {
			return ids.toString();
		} else {
			return ids.substring(0, ids.length() - 1);
		}
	}
}